#include <MCDrawLineSegmentCommand.h>
#include <iostream>
#include <QDoubleValidator>
#include <qlabel.h>

MCDrawLineSegmentCommand::MCDrawLineSegmentCommand(MCCommandManager *commandManager) :
    MCCommand(commandManager),
    _visual(new MCSegmentVisual(presenter, MCLineSegment2D())),
    _currentStage(MCSegmentDrawingStage::SetStartPoint),
    _parametricWindow(nullptr)
{
    presenter->addVisualItem(_visual);
}

MCDrawLineSegmentCommand::~MCDrawLineSegmentCommand()
{
    if (_parametricWindow != nullptr)
    {
        delete _parametricWindow;
    }
}

void MCDrawLineSegmentCommand::onMouseDown(IMouseArgs &args)
{
    if (args.isLeftMousePressed())
    {
        if (_currentStage == MCSegmentDrawingStage::SetStartPoint)
        {
            _visual->update(args.globalPos(), args.globalPos());
            _startPoint = args.globalPos();
            _currentStage = MCSegmentDrawingStage::SetEndPoint;
        }
        else if (_currentStage == MCSegmentDrawingStage::SetEndPoint)
        {
            _visual->update(_startPoint, args.globalPos());
            _endPoint = args.position();
            _currentStage = ParametricWindow;

            _parametricWindow = new MCLineSegmentParametricWindow(this, _visual);
            _parametricWindow->show();
        }
    }
    else if (args.isRightMousePressed())
    {
        terminate();
    }
}

void MCDrawLineSegmentCommand::onMouseUp(IMouseArgs &args)
{
}

void MCDrawLineSegmentCommand::onMouseMove(IMouseArgs &args)
{
    if (_currentStage == MCSegmentDrawingStage::SetEndPoint)
    {
        _visual->update(_startPoint, args.globalPos());
        _endPoint = args.position();
    }
}

void MCDrawLineSegmentCommand::terminate()
{
    presenter->removeVisualItem(_visual);
    finalize();
}